package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.entity.User;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.NursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 护理计划表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-15
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements NursingPlanService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingPlan> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //设置分页参数
        Page<NursingPlan> page = new Page<>(pageNum,pageSize);
        //条件查询构造
        LambdaQueryWrapper<NursingPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name),NursingPlan::getPlanName,name)
                .eq(status!=null,NursingPlan::getStatus,status);

        //查询
        page=getBaseMapper().selectPage(page,wrapper);
        //查询结果缺少creator 只有create id 去user表查询
        page.getRecords().stream().forEach(NursingPlan->{
            User user=userMapper.selectById(NursingPlan.getCreateBy());
            if (user!=null){
                NursingPlan.setCreator(user.getCreator());
            }
        });

        return new PageResponse<>(page);
    }

    //新增
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNursingPlan(NursingPlanDto nursingPlanDto) {
        //从dto中获取plan name数据封装到实体类中
        NursingPlan nursingPlan= BeanUtil.copyProperties(nursingPlanDto,NursingPlan.class);
        //设置status默认值
        nursingPlan.setStatus(1);
        //把数据存入nursingplan表  mp中主键(id)自动返回
        getBaseMapper().insert(nursingPlan);

        //获取集合projectplan信息
        nursingPlanDto.getProjectPlans().stream().forEach(nursingProjectPlan -> {
            //设置计划id  前端没有输入planid  从plan表中获取
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            //存入
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        });
    }

    @Override
    public NursingPlanVo findById(Long id) {
        //直接调用basemapper查询nursingplan的数据
        NursingPlan nursingPlan=getBaseMapper().selectById(id);

        //调用projectplansmapper查询projectplan中的数据
        //从nursing_project_plan中查询nursingPlanVo中的其他内容 从nursing_project中查询projectname
        //nursing_project_plan中没有projectname 所以使用vo
        List<NursingProjectPlanVo>list=nursingProjectPlanMapper.findById(id);

        //合并数据
        NursingPlanVo nursingPlanVo=BeanUtil.copyProperties(nursingPlan,NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePlan(NursingPlanDto nursingPlanDto) {
        //直接从dto中获取nursingplan
        NursingPlan nursingPlan=BeanUtil.copyProperties(nursingPlanDto,NursingPlan.class);
        getBaseMapper().updateById(nursingPlan);

        //先把中间表全部删除再新增
        LambdaQueryWrapper<NursingProjectPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingProjectPlan::getPlanId,nursingPlan.getId());
        nursingProjectPlanMapper.delete(wrapper);
        //新增同新增功能
        nursingPlanDto.getProjectPlans().stream().forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        });
    }

    @Override
    public void deleteById(Long id) {
        //删除关联表信息
        LambdaQueryWrapper<NursingProjectPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingProjectPlan::getPlanId,id);
        nursingProjectPlanMapper.delete(wrapper);

        //执行删除
        getBaseMapper().deleteById(id);
    }
}
